package com.time.workshop.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.time.workshop.API;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.OrderType;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.LaoBanDialog;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.OnClickOKListener;
/**
 * 陪跑页面
 * @author wangfeng
 *
 */
public class RunActivity extends SuperActivity{

	@V
	private ImageView run_order_infoImageView;
	@V
	private TextView tv_run_title , run_order_timeTextView ,run_order_commitOrderTextView;
	@V
	private Button run_order_titlebar_left_btn;
	@V
	private EditText run_order_nameEditText ,run_order_phoneEditText ,run_order_addressEditText,
	run_order_remarksEditText;

	private DatePicker datePicker;  
	private TimePicker timePicker;  
	private AlertDialog dialog;  
	public Calendar time = Calendar.getInstance(Locale.CHINA);
	
	private String runtime,name,phone,address,remark;

	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		setContentView(R.layout.activity_run);
		setView();
	}
	
	private void setView(){
		Injector.getInstance().inject(this);
		run_order_timeTextView.setOnClickListener(this);
		run_order_titlebar_left_btn.setOnClickListener(this);
		run_order_commitOrderTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		super.onClick(view);
		switch (view.getId()) {
		case R.id.run_order_timeTextView:
			dateTimePickerDialog();
			break;
		case R.id.run_order_commitOrderTextView:
			submit();
			break;
		case R.id.run_order_titlebar_left_btn:
			back();
			break;

		default:
			break;
		}
	}
	
	private void submit(){
		runtime = run_order_timeTextView.getText().toString().trim();
		phone = run_order_phoneEditText.getText().toString().trim();
		name = run_order_nameEditText.getText().toString().trim();
		address = run_order_addressEditText.getText().toString().trim();
		remark = run_order_remarksEditText.getText().toString().trim();
		if ("".equals(CommonUtils.isNull(runtime))) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_run_time));
			return;
		}
		if ("".equals(CommonUtils.isNull(name))) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_name));
			return;
		}
		if (!CommonUtils.isMobile(phone)) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_phone));
			return;
		}
		if ("".equals(CommonUtils.isNull(address))) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_address));
			return;
		}
		showConfirmDialog();
	}
	
	private LaoBanDialog dlg;
	/**
	 * 显示  Dailog 
	 */
	private void showConfirmDialog() {

		dlg = new LaoBanDialog(RunActivity.this);
		if (dlg.isShowing()) {
			dlg.dismiss();
			return;
		}
		dlg.onCreate();
		dlg.setOnClickOK(new OnClickOKListener() {

			@Override
			public void setOnClickOK() {
				dlg.dismiss();
				RequestOrder requestOrder = new RequestOrder();
				requestOrder.ORDER_TYPE = OrderType.PPDD.toString();
				requestOrder.reservation_time = runtime;
				requestOrder.contactor_person = name;
				requestOrder.contactor_phone = phone;
				requestOrder.contactor_address = address;
				requestOrder.remark = remark;
				API.submitOrder(requestOrder, RunActivity.this);
			}
		});
	}

	//选择时间
	private AlertDialog dateTimePickerDialog(){  
		LinearLayout dateTimeLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.date_time_picker, null);  
		datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.DatePicker);  
		timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.TimePicker);  
		timePicker.setIs24HourView(true);  

		OnTimeChangedListener timeListener= new OnTimeChangedListener() {  
			@Override  
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {  
				time.set(Calendar.HOUR_OF_DAY, hourOfDay);  
				time.set(Calendar.MINUTE, minute);  

			}  
		};  

		timePicker.setOnTimeChangedListener(timeListener);  
		OnDateChangedListener dateListener = new OnDateChangedListener() {  

			@Override  
			public void onDateChanged(DatePicker view, int year, int monthOfYear,  
					int dayOfMonth) {  
				time.set(Calendar.YEAR, year);  
				time.set(Calendar.MONTH, monthOfYear);  
				time.set(Calendar.DAY_OF_MONTH, dayOfMonth);  
			}  
		};  

		datePicker.init(time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH), dateListener);  
		timePicker.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));  
		timePicker.setCurrentMinute(time.get(Calendar.MINUTE));  

		dialog = new AlertDialog.Builder(this).setTitle("设置日期时间").setView(dateTimeLayout)  
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

						time.set(Calendar.YEAR, datePicker.getYear());  
						time.set(Calendar.MONTH, datePicker.getMonth());  
						time.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());  
						time.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());  
						time.set(Calendar.MINUTE, timePicker.getCurrentMinute());  
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
						run_order_timeTextView.setText(format.format(time.getTime()));
					}  
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

					}  
				}).show();  
		return dialog;  
	} 


}
